using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Xml;
using System.Threading;
using System.IO;
using IReaper.Properties;

namespace IReaper.Configurations
{
    public class LocalizationManager
    {
        static List<CultureInfo> cultureList;

        public static List<CultureInfo> CultureList
        {
            get { return LocalizationManager.cultureList; }
        }
        static CultureInfo currentCulture;

        public static CultureInfo CurrentCulture
        {
            get { return LocalizationManager.currentCulture; }
        }

        static LocalizationManager()
        { 
            //check languages.xml
            if (!File.Exists("Languages.xml"))
            {
                throw new FileNotFoundException(Resources.FileNotFound);
            }
            //instaniate cultureList object 
            cultureList = new List<CultureInfo>();
            //read xml and fill
            XmlReader reader = null;
            try
            {
                reader = XmlReader.Create("Languages.xml", null);

                if (!reader.ReadToFollowing("languages"))
                    throw new ApplicationException(Resources.BadLanguagesXML);
                string currentName = reader.GetAttribute("current");
                if (currentName.Length != 0) //current culture alread set
                {
                    try
                    {
                        currentCulture = new CultureInfo(currentName);
                        Thread.CurrentThread.CurrentUICulture = currentCulture;
                        Application.CurrentCulture = currentCulture;
                        Resources.Culture = currentCulture;
                    }
                    catch
                    {
                        throw new ApplicationException(Resources.BadLanguagesXML);
                    }
                }
                else // or set currentCulture to null
                {
                    currentCulture = null;
                }
                //fill the cultureList
                while (reader.ReadToFollowing("language"))
                {
                    try
                    {
                        string node = reader.ReadString();
                        cultureList.Add(new CultureInfo(node));
                    }
                    catch
                    {
                        throw new ApplicationException(Resources.BadLanguagesXML);
                    }
                }
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        public static void SetCurrentCulture(int index)
        {
            if (index < 0 || index > cultureList.Count - 1)
                return;
            currentCulture = cultureList[index];
            Thread.CurrentThread.CurrentUICulture = currentCulture;
            Resources.Culture = currentCulture;
            //save the selection to languages.xml
            XmlWriter xml = XmlWriter.Create("Languages.xml");
            //write languages
            xml.WriteStartElement("languages");
            xml.WriteAttributeString("current", currentCulture.Name);
            foreach (CultureInfo info in cultureList)
            {
                xml.WriteStartElement("language");
                xml.WriteString(info.Name);
                xml.WriteEndElement();
            }
            xml.WriteEndElement();
            xml.Close();
            return;
        }
    }
}
